package workflow.util;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import workflow.service.ProcessService;



public class ProcessEngine {
	
	private static ProcessEngine instance = null;
	
	private ApplicationContext ctx = null;
	
	protected ProcessEngine(ApplicationContext ctx) {
		this.ctx = ctx;
	}
	    
	protected ProcessEngine(HttpServletRequest request) {
		ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(request.getSession()
						.getServletContext());
	}

	public static ProcessEngine getInstance(HttpServletRequest request) {
		if (instance == null) {
			instance = new ProcessEngine(request);
		}
		return instance;
	}  
	
	public static ProcessEngine getInstance(ApplicationContext ctx) {
		if (instance == null) {
			instance = new ProcessEngine(ctx);
		}
		return instance;
	}  
	
	
	public ProcessService getProcessService(){
		return (ProcessService)ctx.getBean("processService");
	}

}
